package com.acane.instagram.core.design.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.acane.instagram.R
import com.acane.instagram.core.config.Config
import com.acane.instagram.core.design.theme.InstagramLightGray
import com.acane.instagram.core.design.theme.Spacer
import com.acane.instagram.core.extension.clickableNoRipple
import com.acane.instagram.feature.main.TopLevelDestination
import com.acane.instagram.feature.me.ME_ROUTE
import com.acane.instagram.feature.video.VIDEO_ROUTE

@Composable
fun MyNavigationBar(
    destinations: List<TopLevelDestination>,
    currentDestination: String,
    onNavigateToDestination: (Int) -> Unit,
    modifier: Modifier,
    currentUserAvatar: String?, // 新增当前用户头像参数
) {
    val (bottomDividerBackgroundColor, setBottomDividerBackgroundColor) = remember { mutableStateOf(
        InstagramLightGray) }
    Divider(modifier = Modifier.fillMaxWidth()
        .background(Color.Transparent),  // 确保没有背景或阴影,
        color = bottomDividerBackgroundColor
    )
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 动态设置背景颜色
        LaunchedEffect(currentDestination) {
            setBottomDividerBackgroundColor(
                if (currentDestination == VIDEO_ROUTE) Color.Black else InstagramLightGray
            )
        }
        destinations.forEachIndexed { index, destination ->
            val selected = currentDestination == destination.route
            Column (
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .weight(1f)
                    .clickableNoRipple {
                        onNavigateToDestination(index)
                    }
            ) {
                Spacer()
                when (destination.route) {
                    ME_ROUTE -> {
                        AsyncImage(
                            model = currentUserAvatar?.let {
                                "${Config.ADDRESS}avatars/$it"
                            } ?: R.drawable.ic_default_avatar,
                            contentDescription = "用户头像",
                            modifier = Modifier
                                .size(30.dp)
                                .clip(CircleShape)
                                .border(
                                    width = if (selected) 2.dp else 0.dp,
                                    color = Color.Black,
                                    shape = CircleShape
                                ),
                            contentScale = ContentScale.Crop,
                            placeholder = painterResource(R.drawable.ic_default_avatar),
                            error = painterResource(R.drawable.ic_default_avatar)
                        )
                    }

                    else -> {
                        Image(
                            painter = painterResource(
                                id = if (selected) destination.selectedIcon
                                else if (currentDestination == VIDEO_ROUTE) destination.alternativeIcon
                                else destination.unSelectedIcon
                            ),
                            contentDescription = null,
                            modifier = Modifier.size(30.dp)
                        )
                    }
                    }
                }
                Spacer()
            }
        }
    }

@Preview
@Composable
fun MyNavigationBarPreview() {
    MyNavigationBar(
        destinations = TopLevelDestination.entries,
        currentDestination = TopLevelDestination.HOME.route,
        onNavigateToDestination = {},
        modifier = Modifier
            .fillMaxWidth()
            .navigationBarsPadding()
            .background(Color.White),
        currentUserAvatar = "06688594-30bf-4c98-b921-fb3c5e9b90bc_avatar_image.jpg"
    )
}
